//package com.zz.framework.widget.musicplayer
//
///**
// * Copyright © 2025 zhun All rights reserved.
// * Created by ZhaoZhun on 2025/2/20 18:23.
// * @author: ZhaoZhun 1820022519@qq.com
// * @version: V1.0
// */
//import android.app.Service
//import android.content.Intent
//import android.media.MediaPlayer
//import android.media.PlaybackParams
//import android.os.AsyncTask
//import android.os.Binder
//import android.os.Build
//import android.os.IBinder
//import android.os.SystemClock
//import android.text.TextUtils
//import kotlinx.coroutines.*
//import kotlin.coroutines.CoroutineContext
//
//class MediaService : Service(), MediaPlayer.OnCompletionListener,
//    MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener,
//    MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener {
//
//    companion object {
//        private var mMediaPlayer: MediaPlayer? = null
//        private var mProgressTask: ProgressTask? = null
//    }
//
//    private var mCurrPlayPosition = 0
//    private var mPlayUrl: String? = null
//    private var mIsStart = false
//
//    private val mBinder = MediaBinder()
//    private var mMediaStateListener: IMediaStateListener? = null
//
//    inner class MediaBinder : Binder() {
//        fun getService(): MediaService = this@MediaService
//    }
//
//    override fun onBind(intent: Intent): IBinder = mBinder
//
//    override fun onCreate() {
//        super.onCreate()
//        if (mMediaPlayer == null) {
//            mMediaPlayer = MediaPlayer().apply {
//                setOnCompletionListener(this@MediaService)
//                setOnBufferingUpdateListener(this@MediaService)
//                setOnErrorListener(this@MediaService)
//                setOnSeekCompleteListener(this@MediaService)
//                setOnPreparedListener(this@MediaService)
//                setOnInfoListener(this@MediaService)
//            }
//        }
//    }
//
//    fun playMusic(path: String) {
//        mMediaPlayer?.let { player ->
//            try {
//                player.reset()
//                player.setDataSource(path)
//                player.prepareAsync()
//                if (mProgressTask == null) {
//                    mProgressTask = ProgressTask()
//                    mProgressTask?.execute()
//                }
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }
//    }
//
//    fun setMediaStateListener(listener: IMediaStateListener) {
//        mMediaStateListener = listener
//    }
//
//    fun pauseMusic() {
//        mCurrPlayPosition = mMediaPlayer?.currentPosition ?: 0
//        mMediaPlayer?.takeIf { it.isPlaying }?.pause()
//    }
//
//    fun resumeMusic(playPath: String) {
//        playerToPosition(mCurrPlayPosition)
//        if (mPlayUrl.isNullOrEmpty()) {
//            mPlayUrl = playPath
//            playMusic(mPlayUrl!!)
//        } else {
//            mMediaPlayer?.start()
//        }
//    }
//
//    private fun stopMusic() {
//        mMediaPlayer?.apply {
//            stop()
//            release()
//        }
//        mMediaPlayer = null
//    }
//
//    private fun playerToPosition(currPlayPosition: Int) {
//        mMediaPlayer?.let { player ->
//            if (currPlayPosition in 1 until player.duration) {
//                player.seekTo(currPlayPosition)
//            }
//        }
//    }
//
//    override fun onDestroy() {
//        stopMusic()
//        mProgressTask?.stopProgressUpdate()
//        mProgressTask = null
//        super.onDestroy()
//    }
//
//    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//        return super.onStartCommand(intent, flags, startId)
//    }
//
//    override fun onCompletion(mediaPlayer: MediaPlayer) {
//        mMediaStateListener?.onCompletion()
//    }
//
//    override fun onBufferingUpdate(mediaPlayer: MediaPlayer, i: Int) {}
//
//    override fun onError(mediaPlayer: MediaPlayer, what: Int, extra: Int): Boolean {
//        mMediaStateListener?.onError(what, extra)
//        return false
//    }
//
//    override fun onInfo(mp: MediaPlayer, what: Int, extra: Int): Boolean {
//        mMediaStateListener?.onInfo(what, extra)
//        return false
//    }
//
//    override fun onSeekComplete(mediaPlayer: MediaPlayer) {
//        mMediaStateListener?.onSeekComplete()
//    }
//
//    override fun onPrepared(mediaPlayer: MediaPlayer) {
//        mediaPlayer.start()
//        mMediaStateListener?.onPrepared(mediaPlayer.duration)
//        mIsStart = true
//    }
//
//    fun seekToMusic(pos: Int) {
//        mMediaPlayer?.takeIf { mIsStart && pos < it.duration }?.seekTo(pos)
//    }
//
//    fun setPlaySpeed(speed: Float) {
//        mMediaPlayer?.apply {
//            val playbackParams = playbackParams
//            playbackParams.speed = speed
//            this.playbackParams = playbackParams
//        }
//    }
//
//    private inner class ProgressTask : AsyncTask<Void, Void, Void>() {
//        private var mIsUpdate = true
//
//        override fun doInBackground(vararg params: Void?): Void? {
//            while (mIsUpdate) {
//                SystemClock.sleep(1000)
//                publishProgress()
//            }
//            return null
//        }
//
//        override fun onProgressUpdate(vararg values: Void?) {
//            mMediaPlayer?.takeIf { it.isPlaying }?.let { player ->
//                mMediaStateListener?.onProgressUpdate(player.currentPosition, player.duration)
//            }
//            super.onProgressUpdate(*values)
//        }
//
//        fun stopProgressUpdate() {
//            mIsUpdate = false
//        }
//    }
//
//    interface IMediaStateListener {
//        fun onPrepared(duration: Int)
//        fun onProgressUpdate(currentPos: Int, duration: Int)
//        fun onSeekComplete()
//        fun onCompletion()
//        fun onInfo(what: Int, extra: Int): Boolean
//        fun onError(what: Int, extra: Int): Boolean
//    }
//}